using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SmartLink.Infrastructure
{
    public class EnumHelper
    {
        private static Dictionary<string, Type> _DicAllEnumTypes = new Dictionary<string, Type>();

        static EnumHelper()
        {
            //加载所有的枚举
            Assembly asm = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "\\bin\\SmartLink.EnumLibrary.dll");
            Type[] allTypes = asm.GetTypes();
            foreach (Type curType in allTypes)
            {
                _DicAllEnumTypes[curType.Name] = curType;
            }
        }



        /// <summary>
        /// 把枚举转换成字典集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyDefault"></param>
        /// <param name="valueDefault"></param>
        /// <returns></returns>
        public static Dictionary<string, object> EnumListDic<T>(string keyDefault = "", string valueDefault = "")
        {
            var dicEnum = new Dictionary<string, object>();
            var enumType = typeof(T);
            if (!enumType.IsEnum)
            {
                return dicEnum;
            }
            if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项  
            {
                dicEnum.Add(keyDefault, valueDefault);
            }
            var fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组  
            foreach (var item in fieldstrs)
            {
                string description;
                var field = enumType.GetField(item);
                var arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组  
                if (arr != null && arr.Length > 0)
                {
                    description = ((DescriptionAttribute)arr[0]).Description;   //属性描述  
                }
                else
                {
                    description = item;  //描述不存在取字段名称  
                }
                dicEnum.Add(description, (int)Enum.Parse(enumType, item));  //不用枚举的value值作为字典key值的原因从枚举例子能看出来，其实这边应该判断他的值不存在，默认取字段名称  
            }
            return dicEnum;
        }


        /// <summary>
        /// 获取枚举值对应的描述
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enuValue"></param>
        /// <returns></returns>
        public static string GetDesription<T>(int enuValue)
        {
            var enumType = typeof(T);
            if (!enumType.IsEnum)
            {
                return "";
            }
            var fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组  
            foreach (var item in fieldstrs)
            {
                string description;
                var field = enumType.GetField(item);
                var arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组  
                if (arr != null && arr.Length > 0)
                {
                    description = ((DescriptionAttribute)arr[0]).Description;   //属性描述  
                    int value = (int)Enum.Parse(enumType, item);
                    if (enuValue == value)
                        return description;
                }
            }
            return "";
        }

        /// <summary>
		/// 返回枚举项的描述信息
		/// </summary>
		/// <param name="enumItem">枚举项</param>
		/// <returns>枚举项的描述信息</returns>
		public static string GetDesription(Enum enumItem)
        {
            Type enumType = enumItem.GetType();
            if (!enumType.IsEnum)
            {
                return string.Empty;
            }
            string name = Enum.GetName(enumType, enumItem);
            if (name != null)
            {
                // 获取枚举字段。
                var fieldInfo = enumType.GetField(name);
                if (fieldInfo != null)
                {
                    // 获取描述的属性。
                    DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,
                        typeof(DescriptionAttribute), false) as DescriptionAttribute;
                    if (attr != null)
                    {
                        return attr.Description;
                    }
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 返回枚举的所有字段及其描述信息
        /// </summary>
        /// <typeparam name="TEnum">枚举类型</typeparam>
        /// <returns>枚举的所有字段及其描述信息</returns>
        public static Dictionary<TEnum, string> GetFiledsDescriptions<TEnum>()
        {
            if (!typeof(TEnum).IsEnum) throw new Exception("参数类型不正确，不是枚举类型");

            var dictionary = new Dictionary<TEnum, string>();
            FieldInfo[] fieldinfo = typeof(TEnum).GetFields();
            foreach (FieldInfo item in fieldinfo)
            {
                Object[] obj = item.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (obj != null && obj.Length != 0)
                {
                    DescriptionAttribute des = (DescriptionAttribute)obj[0];
                    dictionary.Add((TEnum)item.GetValue(null), des.Description);
                }
            }
            return dictionary;
        }

        /// <summary>
        /// 有描述属性的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyDefault"></param>
        /// <param name="valueDefault"></param>
        /// <returns></returns>
        public static Dictionary<string, object> EnumListToDicWithDescription<T>(string keyDefault = "", string valueDefault = "")
        {
            var dicEnum = new Dictionary<string, object>();
            var enumType = typeof(T);
            if (!enumType.IsEnum)
            {
                return dicEnum;
            }
            if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项  
            {
                dicEnum.Add(keyDefault, valueDefault);
            }
            var fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组  
            foreach (var item in fieldstrs)
            {
                string description;
                var field = enumType.GetField(item);
                var arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组  
                if (arr != null && arr.Length > 0)
                {
                    description = ((DescriptionAttribute)arr[0]).Description;   //属性描述  
                    dicEnum.Add(description, (int)Enum.Parse(enumType, item));
                }
            }
            return dicEnum;
        }

        /// <summary>
        /// 将字符串转换为指定枚举类型
        /// </summary>
        /// <typeparam name="TEnum">指定枚举类型</typeparam>
        /// <param name="str">字符</param>
        /// <returns>字符对应的枚举类型值</returns>
        public static TEnum ToEnum<TEnum>(string str)
        {
            return (TEnum)Enum.Parse(typeof(TEnum), str);
        }

        public static Dictionary<int, string> GetFiledsDescriptionsByEnumKey(string enumKey)
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();
            if (_DicAllEnumTypes.ContainsKey(enumKey))
            {
                Type curType = _DicAllEnumTypes[enumKey];
                FieldInfo[] fieldinfo = curType.GetFields();
                int i = 0;
                foreach (FieldInfo item in fieldinfo)
                {
                    if (item.Name == "value__")
                        continue;

                    int itemValue = (int)item.GetValue(null);
                    string itemName = item+"";//item.GetValue(null).ToString();
                    dic[itemValue] = item.Name;
                }
            }
            return dic;
        }


    }
}
